local qingnang = fk.CreateSkill {

  name = "joyex__qingnang",

  tags = {  },

}



qingnang:addEffect("active", {
  name = "joyex__qingnang",
  anim_type = "control",
  min_card_num = 0,
  min_target_num = 1,
  prompt = "#joyex__qingnang",
  can_use = function(self, player)
    return player:usedSkillTimes(qingnang.name, Player.HistoryPhase) == 0 and not player:isNude()
  end,
  card_filter = function (self, to_select, selected, selected_targets)
    return #selected < #Fk:currentRoom().alive_players - 1
  end,
  target_filter = function(self, player, to_select, selected)
      return #selected < #selected_cards + 1
  end,
  feasible = function(self, player, selected, selected_cards)
    return #selected > 0 and #selected == #selected_cards + 1
  end,
  on_use = function(self, room, effect)
    local room = player.room
    local player = effect.from
    local cards = effect.cards
    if #cards > 0 then
        room:throwCard(cards,qingnang.name,player,player)
    end
    for _, id in ipairs(effect.tos) do
      local p = room:getPlayerById(id)
      if not p.dead then
        room:changeMaxHp(p, 1)
        room:addPlayerMark(p,"joyex__qingnang_"..player.id,1)
      end
      if p:isWounded() then
        room:recover({
          who = p,
          num = 1,
          recoverBy = player,
          skillName = qingnang.name
          })
      end
    end
  end,
})

qingnang:addEffect(fk.TurnStart, {
  refresh_events = {fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
      return target == player and table.find(player.room.alive_players, function(p) return p:getMark("joyex__qingnang_"..player.id) > 0  end)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
      for _, p in ipairs(room.alive_players) do
        if p:getMark("joyex__qingnang_"..player.id) > 0 then
          room:changeMaxHp(p, -1)
          room:removePlayerMark(p,"joyex__qingnang_"..player.id,1)
        end
      end
  end,
})

return qingnang